<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.sql.*" import="Connect.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>Success</h1>
		<hr></hr>
<%
	//Retrieve the submitted information.
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	String retype_password = request.getParameter("retype_password");
	String email = request.getParameter("email");
	String first_name = request.getParameter("first_name");
	String last_name = request.getParameter("last_name");
	String address = request.getParameter("address");
	String city = request.getParameter("city");
	String state = request.getParameter("state");
	String zip_code = request.getParameter("zip_code");
	String telephone = request.getParameter("telephone");
	String hourly_wage = request.getParameter("hourly_wage");
	String level = request.getParameter("level");
	String ssn = request.getParameter("ssn");
	String credit_card_number = "";
	String redirect = "edit_profile.jsp";
	if(session.getAttribute("user_type").equals("2")){
		credit_card_number = request.getParameter("credit_card_number");
	}
	
		// Check if the password is at least 5 characters long and less than
		// or equal 16 characters.
		boolean password_test = password.length() >= 5 && password.length() <= 16;
		// Check if the password and retype password match.
		boolean retype_password_test = password.equals(retype_password);
		// Check if email has an @ character and if there is a period anywhere
		// after the @ character.
		boolean email_test = email.contains("@") && email.indexOf("@") < email.lastIndexOf(".");
		// No tests for these fields yet.
		boolean first_name_test = true;
		boolean last_name_test = true;
		boolean address_test = true;
		boolean city_test = true;
		boolean state_test = true;
		// Check if zip code is an integer.
		boolean zip_code_test = true;
		try {
			int num = Integer.parseInt(zip_code);
		} catch (Exception e) {
			zip_code_test = false;
		}
		// Check if telephone is a long integer.
		boolean telephone_test = true;
		try {
			long num = Long.parseLong(telephone);
		} catch (Exception e) {
			telephone_test = false;
		}
		// Check if credit card number is a 16 digit long integer.
		boolean credit_card_number_test = true;
		if(session.getAttribute("user_type").equals("2")){
			credit_card_number_test = credit_card_number.length() == 16;
			try {
				long num = Long.parseLong(credit_card_number);
			} catch (Exception e) {
				credit_card_number_test = false;
			}
		}
		boolean hourlywage_test = true;
		boolean ssn_test = true;
		if(session.getAttribute("user_type").equals("1")){
			// Check if telephone is a long integer.
			hourlywage_test = true;
			try {
				float num = Float.parseFloat(hourly_wage);
			} catch (Exception e) {
				hourlywage_test = false;
			}
	// Check if the ssn is 9 characters.
			ssn_test = ssn.length() == 9;
			try {
				long num = Long.parseLong(ssn);
			} catch (Exception e) {
				ssn_test = false;
			}
		}
		
		
		// Compile the error messages together.
		String errors = "";
		if (!password_test) {
			errors += "* Password must be between 5 and 16 characters, inclusive.<br />";
		}
		if (!retype_password_test) {
			errors += "* The two passwords do not match.<br />";
		}
		if (!first_name_test) {
			errors += "<br />";
		}
		if (!last_name_test) {
			errors += "<br />";
		}
		if (!address_test) {
			errors += "<br />";
		}
		if (!city_test) {
			errors += "<br />";
		}
		if (!state_test) {
			errors += "<br />";
		}
		if (!zip_code_test) {
			errors += "* ZIP code must be an integer.<br />";
		}
		if (!telephone_test) {
			errors += "* Telephone must be an integer. Do not include the '-'s.<br />";
		}
		if(session.getAttribute("user_type").equals("2")){
			if (!credit_card_number_test) {
				errors += "* Credit card number must be a 16 digit integer. Do not include '-'s.<br />";
			}
		}
		if(session.getAttribute("user_type").equals("1")){
			if (!hourlywage_test) {
				errors += "* Hourly Wage must be a digit integer. Do not include '.'s.<br />";
			}
			if (!ssn_test) {
				errors += "* SSN must be a 9 digit integer. Do not include '-'s.<br />";
			}
		}

		// If there are any errors abort registration and send the user back to the
		// register page. It also saves non-password type data that the user has submitted.
		if (!errors.equals("")) {
			session.setAttribute("register_errors", errors);
			session.setAttribute("username", username);
			session.setAttribute("email", email);
			session.setAttribute("first_name", first_name);
			session.setAttribute("last_name", last_name);
			session.setAttribute("address", address);
			session.setAttribute("city", city);
			session.setAttribute("state", state);
			session.setAttribute("zip_code", zip_code);
			session.setAttribute("telephone", telephone);
			if(session.getAttribute("user_type").equals("2")){
				session.setAttribute("credit_card_number", credit_card_number);
			}
			response.sendRedirect("edit_profile.jsp");
		} else {

			// Getting information and updating the database.
			java.sql.Connection conn = null;
			try {
				// Connect to the database.
				Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
				java.util.Properties sysprops = System.getProperties();
				sysprops.put("user", ConnectionInfo.myUserID());
				sysprops.put("password", ConnectionInfo.myPassword());
				conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);
				conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
				// Update our information.
				
				java.sql.Statement statement = conn.createStatement();
					password = "'" + password + "'";
					email = "'" + email + "'";
					first_name = "'" + first_name + "'";
					last_name = "'" + last_name + "'";
					address = "'" + address + "'";
					city = "'" + city + "'";
					state = "'" + state + "'";
					zip_code = "'" + zip_code + "'";
					telephone = "'" + telephone + "'";
					credit_card_number = "'" + credit_card_number + "'";
					hourly_wage = "'" + hourly_wage + "'";
					level = "'" + level + "'";
					ssn = "'" + ssn + "'";
					statement.executeUpdate("update users set password =" + password
							+", email =" + email
							+", first_name = "+ first_name
							+", last_name = "+ last_name
							+", address ="+ address
							+", city ="+ city
							+", state ="+ state
							+", zip_code ="+ zip_code
							+", telephone ="+ telephone
							+" where username='"+ username+ "'");
					
					statement.executeUpdate("update customers SET credit_card_number ="
							+ credit_card_number + " WHERE customers.username ='"+ username+ "'");
					
					if(session.getAttribute("user_type").equals("1") || session.getAttribute("user_type").equals("0")){
						statement.executeUpdate("update employees SET hourly_wage ="+ hourly_wage 
								+", level =" + level
								+", ssn =" + ssn
								+" WHERE employees.username ='"+ username+ "'");
					}
					
					
					
					out.println("You will be redirected in 5 seconds.<br /><a href=\"" + redirect + "\">Click here if you can't wait!</a>");
					response.setHeader("Refresh", "5; " + redirect);
					
					session.setAttribute("register_errors", null);
					session.setAttribute("username", null);
					session.setAttribute("email", null);
					session.setAttribute("first_name", null);
					session.setAttribute("last_name", null);
					session.setAttribute("address", null);
					session.setAttribute("city", null);
					session.setAttribute("state", null);
					session.setAttribute("zip_code", null);
					session.setAttribute("telephone", null);
					session.setAttribute("credit_card_number", null);
					session.setAttribute("ssn", null);
					session.setAttribute("level", null);
					session.setAttribute("hourly_wage", null);
				} catch (Exception e) {
					e.printStackTrace();
					out.println(e.toString());
				} finally {
				try {
					conn.close();
				} catch (Exception ee) {
					out.println(ee.toString());
				}
				}
		}

%>
</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>
